package com.community.zhihuishequ.view.defense;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.AppBarLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.community.zhihuishequ.R;
import com.community.zhihuishequ.net.entity.defense.DefensePlaceData;
import com.community.zhihuishequ.net.entity.defense.DefensePlaceListData;
import com.community.zhihuishequ.net.entity.defense.DefenseRegionData;
import com.community.zhihuishequ.view.base.BaseActivity;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 营区安防列表
 */
public class DefenseListActivity extends BaseActivity {

    @BindView(R.id.tv_east)
    TextView tvEast;
    @BindView(R.id.tv_east_num)
    TextView tvEastNum;
    @BindView(R.id.rl_east)
    RelativeLayout rlEast;
    @BindView(R.id.tv_south)
    TextView tvSouth;
    @BindView(R.id.tv_south_num)
    TextView tvSouthNum;
    @BindView(R.id.rl_south)
    RelativeLayout rlSouth;
    @BindView(R.id.tv_west)
    TextView tvWest;
    @BindView(R.id.tv_west_num)
    TextView tvWestNum;
    @BindView(R.id.rl_west)
    RelativeLayout rlWest;
    @BindView(R.id.tv_north)
    TextView tvNorth;
    @BindView(R.id.tv_north_num)
    TextView tvNorthNum;
    @BindView(R.id.rl_north)
    RelativeLayout rlNorth;
    @BindView(R.id.rv_content)
    RecyclerView rvContent;
    @BindView(R.id.toolBar)
    Toolbar toolBar;
    @BindView(R.id.appBarLayout)
    AppBarLayout appBarLayout;

    private List<DefensePlaceData> dataList;
    private ContentAdapter adapter;
    private DefenseAdapter defenseAdapter;

    private int eastCount = 0, southCount = 0, westCount = 0, northCount = 0;
    private int eastId = -1, southId = -1, westId = -1, northId = -1;

    public static void startActivity(Context context) {
        Intent intent = new Intent(context, DefenseListActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
        initData();
        getDefenseListRequest();
    }

    private void initView() {
        setContentView(R.layout.activity_list_defense);
        unbinder = ButterKnife.bind(this);
        initActionBar(toolBar, true);
        toolBar.setNavigationIcon(R.drawable.ic_back);
    }

    private void initData() {
        dataList = new ArrayList<>();
        adapter = new ContentAdapter(R.layout.item_defense_place, dataList);
        rvContent.setLayoutManager(new LinearLayoutManager(this));
        rvContent.setAdapter(adapter);
    }

    private class ContentAdapter extends BaseQuickAdapter<DefensePlaceData, BaseViewHolder> {

        public ContentAdapter(int layoutResId, @Nullable List<DefensePlaceData> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, final DefensePlaceData item) {
            helper.setText(R.id.tv_defense_name, item.getName());
            RecyclerView grid = helper.getView(R.id.rl_grid);
            defenseAdapter = new DefenseAdapter(R.layout.item_defense, item.getRegionList());
            grid.setLayoutManager(new GridLayoutManager(DefenseListActivity.this, 3));
            grid.setAdapter(defenseAdapter);
            defenseAdapter.notifyDataSetChanged();
            defenseAdapter.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
                    Intent intent = new Intent(DefenseListActivity.this, DefenseAvtivity.class);
                    intent.putExtra("defenseName", item.getName());
                    intent.putExtra("monitoryPoint", item.getRegionList().get(i).getName());
                    startActivity(intent);
                }
            });
        }
    }

    private class DefenseAdapter extends BaseQuickAdapter<DefenseRegionData, BaseViewHolder> {

        public DefenseAdapter(int layoutResId, @Nullable List<DefenseRegionData> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, DefenseRegionData item) {
            int iconResId;
            switch (item.getStatus()) {
                case DefenseConst.STATUS_NORMAL:
                    iconResId = R.drawable.ic_section_normal;
                    break;
                case DefenseConst.STATUS_ALARM:
                    iconResId = R.drawable.ic_section_alarm;
                    break;
                case DefenseConst.STATUS_REPEAL:
                    iconResId = R.drawable.ic_section_repeal;
                    break;
                case DefenseConst.STATUS_FAULT:
                    iconResId = R.drawable.ic_section_fault;
                    break;
                default:
                    iconResId = R.drawable.ic_section_normal;
                    break;
            }
            TextView tvDefense = helper.getView(R.id.tv_defense);
            tvDefense.setText(item.getName());
            tvDefense.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(iconResId), null, null);
        }
    }

    private void getDefenseListRequest() {
        String json = "{\"placeList\":[{\"id\":1,\"name\":\"东区\",\"regionList\":[{\"id\":10,\"name\":\"体训室后\",\"status\":1,\"type\":1},{\"id\":15,\"name\":\"东车库1\",\"status\":1,\"type\":1},{\"id\":27,\"name\":\"东车库2\",\"status\":1,\"type\":1}],\"unReadNum\":0},{\"id\":2,\"name\":\"南区\",\"regionList\":[{\"id\":4,\"name\":\"杂物房顶\",\"status\":1,\"type\":1},{\"id\":5,\"name\":\"南车库\",\"status\":1,\"type\":1},{\"id\":6,\"name\":\"后山西\",\"status\":1,\"type\":1},{\"id\":25,\"name\":\"足球场西\",\"status\":1,\"type\":1},{\"id\":26,\"name\":\"后山东\",\"status\":1,\"type\":1}],\"unReadNum\":0},{\"id\":3,\"name\":\"西区\",\"regionList\":[{\"id\":7,\"name\":\"大队西南\",\"status\":1,\"type\":1},{\"id\":28,\"name\":\"大队西北\",\"status\":1,\"type\":1}],\"unReadNum\":0},{\"id\":4,\"name\":\"北区\",\"regionList\":[{\"id\":21,\"name\":\"侧门\",\"status\":1,\"type\":2},{\"id\":22,\"name\":\"值班室后\",\"status\":1,\"type\":1},{\"id\":23,\"name\":\"篮球场\",\"status\":1,\"type\":1}],\"unReadNum\":0}]}";
        Gson gson = new Gson();
        DefensePlaceListData data = gson.fromJson(json, DefensePlaceListData.class);
        dataList.clear();
        if (data.getPlaceList() == null) {
            return;
        }
        dataList.addAll(data.getPlaceList());
        //统计东南西北区未读的数量
        for (DefensePlaceData item : data.getPlaceList()) {
            switch (item.getName()) {
                case "东区":
                    eastCount = item.getUnReadNum();
                    eastId = item.getId();
                    break;
                case "南区":
                    southCount = item.getUnReadNum();
                    southId = item.getId();
                    break;
                case "西区":
                    westCount = item.getUnReadNum();
                    westId = item.getId();
                    break;
                case "北区":
                    northCount = item.getUnReadNum();
                    northId = item.getId();
                    break;
                default:
                    break;
            }
        }

        //更新UI
        adapter.notifyDataSetChanged();
        //数量设置
        if (eastCount > 0) {
            tvEast.setTextColor(getResources().getColor(R.color.colorPrimary));
        }
        if (southCount > 0) {
            tvSouth.setTextColor(getResources().getColor(R.color.colorPrimary));
        }
        if (westCount > 0) {
            tvWest.setTextColor(getResources().getColor(R.color.colorPrimary));
        }
        if (northCount > 0) {
            tvNorth.setTextColor(getResources().getColor(R.color.colorPrimary));
        }
        tvEast.setText("东区:" + String.valueOf(eastCount));
        tvSouth.setText("南区:" + String.valueOf(southCount));
        tvWest.setText("西区:" + String.valueOf(westCount));
        tvNorth.setText("北区:" + String.valueOf(northCount));
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
        }
        return super.onOptionsItemSelected(item);
    }
}
